package com.example.mywechatprogram.service;

import com.example.mywechatprogram.entity.NewPassword;
import com.example.mywechatprogram.entity.Response;
import com.example.mywechatprogram.entity.User;
import com.example.mywechatprogram.mapper.UserMapper;
import org.apache.ibatis.binding.BindingException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserService {
    @Autowired
    UserMapper userMapper;
    public Response addUser(User user){
        try {
            int index= userMapper.addUser(user);
            if(index==1) {
                user.setUUID(userMapper.getUUID(user.getNickname()));
                userMapper.insertSculpture(user.getUUID());
                return new Response(Response.SERVICE_RUN_SUCCESSFULLY,user,"注册成功");
            }
            else return new Response(Response.SERVICE_RUN_FIELD,null,"内部错误");
        }catch (DuplicateKeyException e){
            return new Response(10004,null,"昵称重复");
        }catch (Exception e){
            return new Response(Response.SERVICE_RUN_FIELD,null,"内部错误");
        }
    }
    public Response login(User user){
        try {
            user.setUUID(userMapper.login(user));
            if(user.getUUID()!=null){
                return new Response(Response.SERVICE_RUN_SUCCESSFULLY,user,"登录成功");
            }
            else{
                return new Response(Response.SERVICE_RUN_FIELD,null,"账号或者密码错误");
            }
        }catch (BindingException e){
            return new Response(10004,null,"账号或者密码错误");
        }catch (Exception e){
            return new Response(Response.SERVICE_RUN_FIELD,null,"内部错误");
        }
    }
    public Response update(NewPassword newPassword){
        try {
            if (newPassword.getNewPassword().equals(""))return new Response(Response.SERVICE_RUN_SUCCESSFULLY,null,"密码不能为空");
            int index=userMapper.update(newPassword.getNickname(),newPassword.getPassword(),newPassword.getNewPassword());
            if(index==1)return new Response(Response.SERVICE_RUN_SUCCESSFULLY,null,"修改成功");
            else return new Response(10004,null,"账号或者密码错误");
        }catch (BindingException e){
            return new Response(10004,null,"账号或者密码错误");
        }catch (Exception e){
            return new Response(Response.SERVICE_RUN_FIELD,null,"内部错误");
        }
    }
    public Response delete(User user){
        try {
            int index=userMapper.del(user.getNickname(),user.getPassword());
            if(index==1)return new Response(Response.SERVICE_RUN_SUCCESSFULLY,null,"注销成功");
            else return new Response(Response.SERVICE_RUN_SUCCESSFULLY,null,"账号或密码错误");
        }catch (Exception e){
            return new Response(Response.SERVICE_RUN_FIELD,null,"内部错误");
        }
    }
}
